//
//  YuYueOrderViewController.m
//  FD
//
//  Created by Yasin on 2018/10/15.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "YuYueOrderViewController.h"
#import "YuYueOrderModel.h"
#import "YuYueOrderCell.h"

//view
#import "FDNowDataView.h"


//controler
#import "EvaluateController.h"
#import "YuYueShangPinLeiMuViewController.h" //11-23往 “预约点餐商品类目页面跳转”
#import "YYOrderDetailViewController.h" //11-12预约订单详情页面
@interface YuYueOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tv1;
/**数据源*/
@property (nonatomic,  strong) NSMutableArray *dataArr;
/**没有数据占位图*/
@property (nonatomic, strong) FDNowDataView *nodateView;

@end

@implementation YuYueOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    //使用MJRefresh 如果自己在修改了contentInset会有bug的
    CGFloat y = FD_NavH + 35;
    CGFloat h = SCREEN_HEIGHT - y - Height_TabBar;
    self.tv1.frame = CGRectMake(0, y, SCREEN_WIDTH, h);
//    self.tv1.contentInset = UIEdgeInsetsMake(40, 0, 44, 0);
//    self.tv1.frame = self.view.bounds;
    [self.view addSubview:self.tv1];
    self.tv1.tableFooterView = [UIView new];
    
    //请求数据
//    [self setupRefresh];
    
    [self.view addSubview:self.nodateView];
    [self.nodateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 30));
        make.centerY.equalTo(self.view.mas_centerY);
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    FDLog(@"来了:%s====%d",__FUNCTION__,__LINE__);
    [self setupRefresh];
}

- (void)setupRefresh {

    FDRefreshHader *refreshHeader = [FDRefreshHader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tv1.mj_header = refreshHeader;
    [self.tv1.mj_header beginRefreshing]; //进入刷新状态
}


#pragma mark - getter
- (UITableView *)tv1 {
    if (!_tv1) {
        _tv1 = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tv1.delegate = self;
        _tv1.dataSource = self;
        
        //注册cell

        [_tv1 registerClass:[YuYueOrderCell class] forCellReuseIdentifier:@"YuYueOrderCell"];
        _tv1.separatorStyle = UITableViewCellSeparatorStyleNone; //取消线
        _tv1.showsVerticalScrollIndicator = YES;
    }
    return _tv1;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (FDNowDataView *)nodateView {
    if (!_nodateView) {
        _nodateView = [[FDNowDataView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        _nodateView.hidden = YES; //默认隐藏
    }
    return _nodateView;
}

#pragma mark - UITableViewDelegate


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YuYueOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YuYueOrderCell"];
    
    cell.model = self.dataArr[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 202;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//取出对应的mode ，根据status进行判断
    YuYueOrderModel *model = self.dataArr[indexPath.row];
    
    if ([NSString isEmptyString:model.userOrderId]) { //跳转预约点餐页面
        //跳转预约点餐
        YuYueShangPinLeiMuViewController *yuyueVC = [[YuYueShangPinLeiMuViewController alloc] init];
        yuyueVC.yuyueId1 = model.yuyueId;
        yuyueVC.hidesBottomBarWhenPushed = YES;//隐藏底部tabbar
        [self.navigationController pushViewController:yuyueVC animated:YES];
    }else { //查看详情
        YYOrderDetailViewController *detailVC = [[YYOrderDetailViewController alloc] init];
        detailVC.hidesBottomBarWhenPushed = YES;//隐藏底部tabbar

        detailVC.userOrderId = model.userOrderId; //预约订单id
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    
    
//    EvaluateController *evaluateVC = [[EvaluateController alloc] init];
//    [self.navigationController pushViewController:evaluateVC animated:YES];
}


#pragma mark - 请求数据
- (void)loadData {
    [FDHttpAPi requestDDShopReservationSuccessBlock:^(id responseObjc) {
        [self.tv1.mj_header endRefreshing];//结束刷新 

        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            
            [self.dataArr removeAllObjects]; //1-19清空反之越刷越多
            
            //解析数据：数组非空才进行处理
            NSArray *dataArr = responseObjc[@"data"];
            if (dataArr.count == 0) {
                FDLog(@"数组为空，不做处理");
                self.nodateView.hidden = NO; //没有数据占位图
                return ;
            }
            
            self.nodateView.hidden = YES;
            
            // 字典转模型
            for (NSDictionary *dic in dataArr) {
                YuYueOrderModel *model = [YuYueOrderModel mj_objectWithKeyValues:dic];
                [self.dataArr addObject:model];
            }
            
            //刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tv1 reloadData];
            });
        }else { //toast
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        [self.tv1.mj_header endRefreshing];//结束刷新
    }];
}

@end
